<?php

namespace app\index\controller;

use think\Controller;

//use think\Log;
class Lists extends Base
{
    public function index()
    {
        $categoryParentId = 0;
        $firstCategoryIds = [];
        $sedcategorys = [];
        //获取一级分类
        $categorys = model('Category')->getNormalCategoryByParentId();
        foreach ($categorys as $category) {
            $firstCategoryIds[] = $category->id;
        }
        $id = input('id', 0, 'intval');
        $data = [];
        if (in_array($id, $firstCategoryIds)) {
            //一级分类
            $categoryParentId = $id;
            $data['category_id'] = $id;
        } elseif ($id) {
            //二级分类
            $category = model('Category')->where('id', $id)->find();
            if (!$category || $category->status != 1) {
                $this->error('数据不合法');
            }
            $categoryParentId = $category->parent_id;
            $data['se_category_id'] = $id;
        } else {
            //没有传递
            $categoryParentId = 0;
        }
        //获取父类下所有子类
        if ($categoryParentId) {
            $sedcategorys = model('Category')->getNormalCategoryByParentId($categoryParentId);
        }
        $orders = [];
        //排序
        $order_sales = input('order_sales', '');
        $order_price = input('order_price', '');
        $order_time = input('order_time', '');
        if (!empty($order_sales)) {
            $orderflag = 'order_sales';
            $orders['order_sales'] = $order_sales;
        } elseif (!empty($order_price)) {
            $orderflag = 'order_price';
            $orders['order_price'] = $order_price;
        } elseif (!empty($order_time)) {
            $orderflag = 'order_time';
            $orders['order_time'] = $order_time;
        } else {
            $orderflag = '';
        }
        //根据销量价格发布时间排序
        $deals = model('Deal')->getDealByConditions($data,$orders);
        return $this->fetch('', [
            'categorys' => $categorys,
            'sedcategorys' => $sedcategorys,
            'id' => $id,
            'categoryParentId' => $categoryParentId,
            'orderflag' => $orderflag,
            'deals' => $deals,
        ]);
    }
}
